package com.yaqian.wolf.controller;


import com.yaqian.wolf.comment.JwtUtil;
import com.yaqian.wolf.comment.Result;
import com.yaqian.wolf.entity.Floorlist;
import com.yaqian.wolf.entity.User;
import com.yaqian.wolf.repository.FloorlistRepisitory;
import com.yaqian.wolf.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/rent")
public class LoginHandler {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private com.yaqian.wolf.repository.FloorlistRepisitory FloorlistRepisitory;


    @GetMapping("/floorlist")
    public List<Floorlist> findfloor(){
        return FloorlistRepisitory.findAll();
    }
    @GetMapping("/userlist")
    public List<User> finduser(){
        return userRepository.findAll();
    }
    @PostMapping("/login")
    public Result identify(@RequestBody User user){

            Result message =new Result();
        try {
            System.out.println(user);
            Boolean flag =user.getPassword().equals(userRepository.findById(user.getName()).get().getPassword());
            if(flag)
            {
                System.out.println(user.getName());
                User  theuser =userRepository.findById(user.getName()).get();
                Map<String,Object> map = new HashMap<String,Object>();
                map.put("user", theuser);
                map.put("token", JwtUtil.generateToken(theuser.getName()));
                message.setData(map);
            }
            return message;
        }catch (Exception e){
            message.addError();
            return message;
        }
    }

//    @PostMapping("/identify")
//    public Boolean identify(@RequestBody User user ) {
//        try {
////            return user.equals(FloorlistRepisitory.findById(user.getName()).get());
//            return true;
//        }catch (Exception e){
//            return false;
//        }
//    }

}
